package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2025/1/3 10:57
 * @Version 1.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /*添加购物车功能实现*/
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        /*添加购物车思路：
        * 1、判断当前商品是否存在购物车中
        * 2、如果已经存在，就更新数量，数量加一
        * 3、如果不存在，插入数据，数量就是一
        * 4、再判断当前添加到购物车的是菜品还是套餐
        *       1、添加到购物车的是菜品
        *       2、添加到购物车的是套餐*/

        //1、判断当前商品是否存在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //只能查询自己购物车的数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getById(shoppingCart);
        if (shoppingCart != null && shoppingCartList.size() > 0){
            //2、如果已经存在，就更新数量，数量加一
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
        else {
            //3、如果不存在，插入数据，数量就是一

            //4、再判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                //添加到购物车的数菜品
                Dish dish = dishMapper.getByDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else {
                //添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /*用户查看购物车功能实现*/
    @Override
    public List<ShoppingCart> getByUserId() {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getById(ShoppingCart.builder()
        .userId(BaseContext.getCurrentId())
        .build());
        return shoppingCartList;
    }

    /*用户清空购物车功能实现*/
    @Override
    public void delete() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }


    /*删除购物车中的一个菜品功能实现*/
    @Override
    public void delOne(ShoppingCartDTO shoppingCartDTO) {
        /*删除购物车中的菜品实现思路：
        * 1、判断删除的是菜品还是套餐
        * 2、根据菜品或者套餐ID来把对应的数量减一*/

        //查询当前用户的购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getById(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() > 0){
            shoppingCart = shoppingCartList.get(0);

            Integer number = shoppingCart.getNumber();
            if (number == 1){
                //当前商品再购物车中的记录份数为1，直接删除当前记录
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
            else {
                //当前商品再购物车中的份数不为一，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() -1 );
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
